package com.example.Windturbine.Listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.example.Windturbine.Controller.ViewObject.Excel.ManufactureExcel;
import com.example.Windturbine.Controller.ViewObject.Excel.TransportExcel;
import com.example.Windturbine.Error.BusinessException;
import com.example.Windturbine.Error.EmBusinessError;
import com.example.Windturbine.Service.ManufactureService;
import com.example.Windturbine.Service.Model.ManufactureModel;
import com.example.Windturbine.Service.Model.TransportModel;
import com.example.Windturbine.Service.TransportService;
import lombok.SneakyThrows;
import org.springframework.beans.BeanUtils;

/**
 * @Author 丁永新
 * @Date 2021/12/17
 */
public class ExceManufactureListener extends AnalysisEventListener<ManufactureExcel> {


    ManufactureService manufactureService;

    Integer userid;


    public ExceManufactureListener(ManufactureService materialService, Integer userid) {
        this.manufactureService=materialService;
        this.userid=userid;
    }



    //存入数据库

    @SneakyThrows
    @Override
    public void invoke(ManufactureExcel manufactureExcel, AnalysisContext analysisContext) {
        try{
            ManufactureModel materialModel=convertFromExcel(manufactureExcel);
            materialModel.setLevel(userid);
            manufactureService.submitSingle(materialModel);
            System.out.println(materialModel.getName());
        }catch (Exception e){
            throw new BusinessException(EmBusinessError.UNKNOWN_ERROR,"Excel文件格式错误!");
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }

    private ManufactureModel convertFromExcel(ManufactureExcel manufactureExcel){
        ManufactureModel manufactureModel=new ManufactureModel();
        manufactureModel.setLevel(this.userid);
        BeanUtils.copyProperties(manufactureExcel,manufactureModel);
        return manufactureModel;
    }


}

